package com.ruoyi.web.controller.bus.appletsController;

import com.ruoyi.bus.constant.BookIngTypeEnum;
import com.ruoyi.bus.constant.UserTypeEnum;
import com.ruoyi.bus.domain.WitAppoint;
import com.ruoyi.bus.domain.WitComment;
import com.ruoyi.bus.domain.WitNotice;
import com.ruoyi.bus.service.IApiService;
import com.ruoyi.bus.vo.IntoCarVo;
import com.ruoyi.bus.vo.LoginVo;
import com.ruoyi.bus.vo.RegisterVo;
import com.ruoyi.bus.wechat.WechatApi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 预约用户小程序api接口
 * @Author caiyanshan
 * @Date 2021/4/15 8:53
 * @Param
 * @Return
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerUserApiController extends BaseController {
  
  @Autowired
  private IApiService apiService;

  /**
   * @Description 登录功能
   * @Author caiyanshan
   * @Date 2021/4/15 14:24
   * @Param []
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @PostMapping("login")
  public AjaxResult login(@Valid @RequestBody LoginVo loginVo){
    loginVo.setUserType(UserTypeEnum.Customer_User.ordinal());//普通用户
    return apiService.login(loginVo);
  }
  
  /**
   * @Description 注册功能
   * @Author caiyanshan
   * @Date 2021/4/15 14:24
   * @Param []
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @PostMapping("register")
  @Log(title = "用户端注册", businessType = BusinessType.INSERT)
  public AjaxResult register(@Valid @RequestBody RegisterVo registerVo){
    registerVo.setUserType(UserTypeEnum.Customer_User.ordinal());//普通用户
    return apiService.register(registerVo);
  }
  /**
   * @Description 预约
   * @Author caiyanshan
   * @Date 2021/4/16 10:33
   * @Param [witAppoint]
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @PostMapping("makeBookIng")
  public AjaxResult makeBookIng(@RequestBody WitAppoint witAppoint){
    AjaxResult ajax = null;
    try{
      ajax = apiService.makeBookIng(witAppoint);
    }catch(Exception e){
      return AjaxResult.error(e.getMessage());
    }
    return ajax;
  }
  
  /**
   * @Description 取消预约
   * @Author caiyanshan
   * @Date 2021/4/16 10:33
   * @Param [appointId]
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @GetMapping("cancelBookIng")
  public AjaxResult cancelBookIng(@RequestParam Long appointId){
    return apiService.cancelBookIng(appointId);
  }

  /**
   * 我的预约:分三种 0已预约 1已取消 2已完成
   * @param type
   * @return
   */
  @GetMapping("myBookIng")
  public AjaxResult myBookIng(@RequestParam Integer type){
    BookIngTypeEnum paramEnum = null;
    if(type.intValue() != -1){//-1是查除了已预约 已取消 已完成状态外的数据
      for(BookIngTypeEnum enumItem : BookIngTypeEnum.values()){//必须系统内配置,否则无法调用
        if(enumItem.ordinal() == type.intValue()){
          paramEnum = enumItem;
        }
      }
      if(paramEnum == null){
        return AjaxResult.error("请输入正常范围值内得参数");
      }
    }
    return apiService.myBookIng(paramEnum);
  }

  /**
   * 获取预约详情
   * @param appointId
   * @return
   */
  @GetMapping("getBookingDetail")
  public AjaxResult getBookingDetail(@RequestParam Long appointId){
    return apiService.getBookingDetail(appointId);
  }

  /**
   * 获取点评类型
   * @return
   */
  @GetMapping("listCommentType")
  public AjaxResult listCommentType(){
    return apiService.listCommentType();
  }

  /**
   * 提交点评
   * @param witComment
   * @return
   */
  @PostMapping("submitComment")
  public AjaxResult submitComment(@RequestBody @Valid WitComment witComment){
    return apiService.submitComment(witComment);
  }

  /**
   * 首页数据
   * 数据分两部分,
   * 1、通知
   * 2、是预约
   * 两部分分开写,之后整合为一组数据返回给前台
   * 若后续需求有改动，可直接屏蔽某一部分即可
   * @return
   */
  @GetMapping("listHomeAllData")
  public AjaxResult listHomeAllData(){
    List<WitAppoint> witAppointList = apiService.listHomeBookIngData();
    List<WitNotice> witNoticeList = apiService.listHomeNoticeData();
    List<BaseEntity> tempList = new ArrayList<BaseEntity>();
    tempList.addAll(witAppointList);
    tempList.addAll(witNoticeList);
    tempList.sort((o1, o2) -> (int) (o2.getCreateTime().getTime() - o1.getCreateTime().getTime()));
    return AjaxResult.success(tempList);
  }

  /**
   * 首页预约数据
   * @return
   */
  @GetMapping("listHomeBookIngData")
  public AjaxResult listHomeBookIngData(){
    return AjaxResult.success(apiService.listHomeBookIngData());
  }

  /**
   * 首页通知消息
   * @return
   */
  @GetMapping("listHomeNoticeData")
  public AjaxResult listHomeNoticeData(){
    return AjaxResult.success(apiService.listHomeNoticeData());
  }

  /**
   * 通知详情
   * @param noticeId
   * @return
   */
  @GetMapping("getNoticeDetail")
  public AjaxResult getNoticeDetail(@RequestParam Long noticeId){
    return apiService.getNoticeDetail(noticeId);
  }
  
  
  /**
   * @Description 用户对应参数为2
   * @Author caiyanshan
   * @Date 2021/4/23 14:56
   * @Param []
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @GetMapping("jsCodeToSession")
  public AjaxResult jsCodeToSession(String jsCode){
    Integer type = 2;
    return WechatApi.getOpenIdByCode(jsCode, type);
  }
  
  /**
   * @Description 我的常用路线
   * @Author caiyanshan
   * @Date 2021/4/25 9:23
   * @Param []
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @GetMapping("myCommonRoute")
  public AjaxResult myCommonRoute(){
    return apiService.myCommonRoute();
  }
  
  /**
   * @Description 临时预约接口
   * @Author caiyanshan
   * @Date 2021/4/29 13:53
   * @Param [witAppoint]
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @PostMapping("tempMakeBooking")
  public AjaxResult tempMakeBooking(@RequestBody WitAppoint witAppoint){
    return apiService.tempMakeBooking(witAppoint);
  }
  
  /**
   * @Description 扫码上车
   * @Author caiyanshan
   * @Date 2021/4/29 16:26
   * @Param [intoCarVo]
   * @Return com.ruoyi.common.core.domain.AjaxResult
   */
  @PostMapping("intoCarVo")
  public AjaxResult intoCar(@RequestBody IntoCarVo intoCarVo){
    return apiService.intoCar(intoCarVo);
  }

}
